import http from './http'

export interface Ticket {
  id: string
  _id?: string  // Compatibility alias
  ticketNumber: string
  title: string
  description: string
  type: 'incident' | 'alert' | 'maintenance' | 'request'
  priority: 'critical' | 'high' | 'medium' | 'low'
  status: 'open' | 'in_progress' | 'pending' | 'resolved' | 'closed'
  alertId?: string
  createdBy: {
    _id: string
    username: string
    email: string
  }
  assignedTo?: {
    _id: string
    username: string
    email: string
  }
  watchers: Array<{
    _id: string
    username: string
    email: string
  }>
  comments: Array<{
    userId: string
    userName: string
    content: string
    isInternal: boolean
    createdAt: string
  }>
  statusHistory: Array<{
    from: string
    to: string
    userId: string
    userName: string
    reason?: string
    changedAt: string
  }>
  resolution?: {
    solution: string
    resolvedBy: string
    resolvedAt: string
    rootCause?: string
    preventiveMeasures?: string
  }
  sla?: {
    responseDeadline: string
    resolutionDeadline: string
    isBreached: boolean
  }
  tags: string[]
  createdAt: string
  updatedAt: string
  closedAt?: string
}

export interface TicketFilter {
  status?: string[]
  priority?: string[]
  type?: string[]
  assignedTo?: string
  createdBy?: string
  tags?: string[]
  search?: string
  page?: number
  pageSize?: number
}

export interface CreateTicketDto {
  title: string
  description: string
  type: 'incident' | 'alert' | 'maintenance' | 'request'
  priority: 'critical' | 'high' | 'medium' | 'low'
  alertId?: string
  assignedTo?: string
  tags?: string[]
}

export interface UpdateTicketDto {
  title?: string
  description?: string
  priority?: 'critical' | 'high' | 'medium' | 'low'
  assignedTo?: string
  tags?: string[]
}

export interface TicketStatistics {
  total: number
  byStatus: {
    open: number
    inProgress: number
    pending: number
    resolved: number
    closed: number
  }
  byPriority: {
    critical: number
    high: number
    medium: number
    low: number
  }
  slaBreached: number
}

// 获取工单列表
export const getTickets = (filter?: TicketFilter) => {
  return http.get('/tickets', { params: filter })
}

// 获取工单详情
export const getTicketById = (id: string) => {
  return http.get(`/tickets/${id}`)
}

// 创建工单
export const createTicket = (data: CreateTicketDto) => {
  return http.post('/tickets', data)
}

// 更新工单
export const updateTicket = (id: string, data: UpdateTicketDto) => {
  return http.put(`/tickets/${id}`, data)
}

// 更新工单状态
export const updateTicketStatus = (
  id: string,
  status: 'open' | 'in_progress' | 'pending' | 'resolved' | 'closed',
  reason?: string
) => {
  return http.put(`/tickets/${id}/status`, { status, reason })
}

// 分配工单
export const assignTicket = (id: string, assignedTo: string) => {
  return http.post(`/tickets/${id}/assign`, { assignedTo })
}

// 添加评论
export const addComment = (id: string, content: string, isInternal: boolean = false) => {
  return http.post(`/tickets/${id}/comment`, { content, isInternal })
}

// 解决工单
export const resolveTicket = (
  id: string,
  solution: string,
  rootCause?: string,
  preventiveMeasures?: string
) => {
  return http.post(`/tickets/${id}/resolve`, { solution, rootCause, preventiveMeasures })
}

// 关闭工单
export const closeTicket = (id: string) => {
  return http.post(`/tickets/${id}/close`)
}

// 删除工单
export const deleteTicket = (id: string) => {
  return http.delete(`/tickets/${id}`)
}

// 获取工单统计
export const getTicketStatistics = (filter?: Partial<TicketFilter>) => {
  return http.get('/tickets/statistics', { params: filter })
}
